package com.mfl.controller.admin;

import com.mfl.common.PageResult;
import com.mfl.common.Result;
import com.mfl.pojo.dto.UserDTO;
import com.mfl.pojo.dto.UserPageQueryDTO;
import com.mfl.pojo.vo.UserVO;
import com.mfl.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController("adminUserController")
@RequestMapping("/cky/admin/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加用户
     * @param userDTO
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody UserDTO userDTO){
        log.info("添加用户：{}", userDTO);
        userService.insert(userDTO);
        return Result.success("添加成功");
    }

    /**
     * 分页查询用户
     * @param userPageQueryDTO
     * @return
     */
    @GetMapping("/pageQuery")
    public Result<PageResult> pageQuery(UserPageQueryDTO userPageQueryDTO){
        log.info("分页查询用户：{}", userPageQueryDTO);
        PageResult pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success("", pageResult);
    }

    /**
     * 修改用户状态
     * @param status
     * @param requestBody
     * @return
     */
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, @RequestBody Map<String, Object> requestBody){
        // 直接用Integer id来接收参数始终为空，转为map接收
        Integer id = (Integer) requestBody.get("id");
        log.info("修改用户状态：id：{}, status：{}", id, status);
        userService.updateStatus(status, id);
        return Result.success("修改成功");
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除用户：{}", ids);
        userService.batchDelete(ids);
        return Result.success("删除成功");
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUserById")
    public Result<UserVO> getUserById(int id){
        log.info("根据id查询用户：{}", id);
        UserVO vo = userService.getUserById(id);
        return Result.success("", vo);
    }

    /**
     * 修改用户信息
     * @param userDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody UserDTO userDTO){
        log.info("修改用户信息：{}", userDTO);
        userService.update(userDTO);
        return Result.success("修改成功");
    }
}
